package com.sam.im.heiqun.uis.activities;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.GetmoneyEntivity;
import com.sam.im.heiqun.entities.ImFriendEntivity;
import com.sam.im.heiqun.entities.PayTransferEntivity;
import com.sam.im.heiqun.utils.RequestRSAUtils;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.sam.im.heiqun.zxing.encode.EncodingHandler;
import com.google.gson.Gson;
import com.google.zxing.WriterException;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;
import com.yuyh.library.utils.GlideCircleTransform;
import com.yuyh.library.utils.GlideUtils;

import java.io.UnsupportedEncodingException;

import butterknife.BindView;

/**
 * 可传值 ：
 * “qrString” - - - - - -  要生成二维码字符串       不传 默认 个人id
 * “type” - - - - - - - -  0 个人       1 群组      不传默认为 个人
 * “img” - - - - - - - --  中心图片，               没有默认显示 logo
 */
public class QRcode extends BaseSwipeBackActivity {

    private static final String TAG = QRcode.class.getSimpleName();

    @BindView(R.id.qr_code)
    ImageView mQRcode;
    @BindView(R.id.center_img)
    ImageView centerImg;

    @BindView(R.id.text_details)
    TextView textDetails;

    String userId;
    private int QRtype; // 0 个人    1 群组
    private String centerImgStr;
    private GetmoneyEntivity getmoneyEntivity ;
    private PayTransferEntivity payTransferEntivity ;
    @Override
    public int getContentViewId() {
        return R.layout.activity_qrcode;
    }

    @Override
    protected String getTitleText() {
        return getResources().getString(R.string.qr_code);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {

        initUI();
    }

    /**
     * 更新界面
     */
    private void initUI() {
        ImFriendEntivity myInfo = ToolsUtils.getUser();

        Intent intent = getIntent();
        userId = intent.getStringExtra("qrString");
        QRtype = intent.getIntExtra("type", 0);
        centerImgStr = intent.getStringExtra("img");

        //付款码
        payTransferEntivity= (PayTransferEntivity) intent.getSerializableExtra("paymoney");
        getmoneyEntivity = (GetmoneyEntivity) intent.getSerializableExtra("getmoney");
        /*if (payTransferEntivity!=null&&QRtype==-11){
            String amount1 = RequestRSAUtils.getRsaContent(payTransferEntivity.getAmount());
            payTransferEntivity.setAmount(amount1);
        }else if(getmoneyEntivity!=null&&QRtype==-1){
        //收款码
            String amount = RequestRSAUtils.getRsaContent(getmoneyEntivity.getAmount());
            getmoneyEntivity.setAmount(amount);
        }*/
        if ("".equals(userId) || userId == null) {
            userId = "" + myInfo.getId();
        }
        Bitmap bitmap = null;
        if (QRtype == 0) {
            textDetails.setText(R.string.richscan_up_qr_code_add_me_be_friend);
            bitmap = create2Code("a_" + userId);
        } else if (QRtype ==1){
            textDetails.setText(R.string.richscan_up_qr_code_add_our_group);
            bitmap = create2Code("b_" + userId);
        } else if (QRtype ==-1){
            textDetails.setText(getResources().getString(R.string.no_friend_qrcode_pay));
            String key = new Gson().toJson(getmoneyEntivity);
            bitmap = create2Code("c_" +key);
        }else if (QRtype==-11){
            textDetails.setText(getResources().getString(R.string.no_friend_qrcode_getm));
            String key1 = new Gson().toJson(payTransferEntivity);
            bitmap = create2Code("d_" +key1);
        }
        if (getmoneyEntivity != null) {
            centerImgStr = getmoneyEntivity.getReceiptHeadUrl();
        }
        if (payTransferEntivity != null) {
            centerImgStr = payTransferEntivity.getPaymentHeadUrl();
        }
        mQRcode.setImageBitmap(bitmap);

        if (centerImgStr == null || "".equals(centerImgStr)) {
            GlideUtils.loadLocalImage(QRcode.this, R.drawable.logo1, centerImg);
        } else {
            Glide.with(this).load(centerImgStr).placeholder(R.drawable.logo1).error(R.drawable.logo1).dontAnimate().
                    transform(new GlideCircleTransform(this)).into(centerImg);
        }
    }

    /**
     * 生成二维码
     */
    private Bitmap create2Code(String key) {
        Bitmap qrCode = null;

        try {
            qrCode = EncodingHandler.create2Code(key, dpTOpx(260));
        } catch (WriterException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return qrCode;
    }

    /**
     * dp 转换为 px
     */
    public int dpTOpx(float dpValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
